import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';

import '../../config/kq_global.dart';
import '../../resources/images.dart';

/// 通用空布局
/// @author 周卓
class KqEmptyView extends StatelessWidget {
  /// 图片路径
  final String? imagePath;

  /// 文字
  final String? text;

  /// 文字颜色
  final Color textColor;

  /// 自定义底部控件
  final Widget? customWidget;

  /// 是否自动根据当前布局是否可滚动，自动撑满剩余空间。
  final bool autoSliver;

  /// 是否强制自动撑满剩余空间。
  final bool forceSliver;

  /// 滚动物理特性
  final ScrollPhysics? physics;

  /// 当[autoSliver]=true时，是否具有滚动主体，一般不用设置
  final bool hasScrollBody;

  /// 当[autoSliver]=true时，是否可填充滚动区域，一般不用设置
  final bool fillOverscroll;

  /// 空布局背景色
  final Color? backgroundColor;

  /// 底部提示文字，如果传了这个，代表空布局的高度是充满剩余空间的。
  final String? bottomHintString;

  const KqEmptyView({
    Key? key,
    this.imagePath,
    this.text,
    this.autoSliver = true,
    this.forceSliver = false,
    this.physics,
    this.hasScrollBody = true,
    this.fillOverscroll = false,
    this.textColor = KqThemeColors.text8C,
    this.customWidget,
    this.backgroundColor,
    this.bottomHintString,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (forceSliver ||
        (autoSliver && tryGetContextScrollController(context) != null)) {
      return CustomScrollView(
        physics: physics,
        slivers: [
          SliverFillRemaining(
            hasScrollBody: hasScrollBody,
            fillOverscroll: fillOverscroll,
            child: _buildEmptyView(),
          )
        ],
      );
    } else {
      return _buildEmptyView();
    }
  }

  /// 判断外层是否能滚动
  tryGetContextScrollController(BuildContext context) {
    try {
      return PrimaryScrollController.of(context);
    } catch (e) {
      return null;
    }
  }

  /// 空布局
  _buildEmptyView() {
    Widget result = Stack(
      clipBehavior: Clip.none,
      children: [
        Align(
          alignment: Alignment.center,
          child: Image.asset(
            imagePath ?? Images.commonPicZanwushuju,
            width: 154.r,
            height: 154.r,
            fit: BoxFit.fill,
            package: imagePath != null ? null : KqGlobal.packageName,
          ),
        ),
        Positioned(
            left: 0,
            right: 0,
            top: 118.r,
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 10.r),
              child: Text(
                text ?? KqIntl.currentResource.noDataTip,
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 14.sp, color: textColor),
              ),
            ))
      ],
    );
    if (bottomHintString != null) {
      result = Column(
        mainAxisSize: MainAxisSize.max,
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                result,
                if (customWidget != null) customWidget!,
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.only(left: 24.r, right: 24.r, bottom: 12.r),
            child: Text(
              bottomHintString ?? '',
              style: TextStyle(fontSize: 12.sp, color: KqThemeColors.text8C),
            ),
          ),
        ],
      );
    } else {
      result = Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          result,
          if (customWidget != null) customWidget!,
        ],
      );
    }
    return Container(
      color: backgroundColor,
      alignment: Alignment.center,
      child: result,
    );
  }
}
